package com.mvvm.ktnet.ui.testChart

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.Log
import com.android.ktx.context.dp
import com.android.ktx.context.dp2px
import com.android.ktx.context.getColor2
import com.android.ktx.context.getDimen
import com.drake.logcat.LogCat
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.formatter.ValueFormatter
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.utils.ViewPortHandler
import com.mvvm.ktnet.R

class SportCardBarView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyle: Int = 0
) : BarChart(context, attributeSet, defStyle) {
    override fun init() {
        super.init()
        this.mRenderer = SportCardBarChartRenderer(this, this.mAnimator, this.mViewPortHandler)

        //处理图形

        setBackgroundColor(context.getColor2(R.color.white))
        setNoDataText("暂无数据")
        setNoDataTextColor(context.getColor2(R.color.black))
        mInfoPaint.textSize = 14.dp2px.toFloat()

        setNoDataText("暂无数据")
        //背景颜色
        setBackgroundColor(Color.WHITE)
        //是否显示网格背景
        setDrawGridBackground(false)
        //显示每条背景阴影
        setDrawBarShadow(false)
        setTouchEnabled(false)
        isDragEnabled = false
        setScaleEnabled(false)
        isScaleXEnabled = false
        isScaleYEnabled = false
        setPinchZoom(false)
        isDoubleTapToZoomEnabled = false
        isDragDecelerationEnabled = false
        description.isEnabled = false
        setVisibleXRangeMaximum(8f)
        //图例
        legend.isEnabled = false
        axisRight.isEnabled = false

        val weekString = mutableListOf(
            context.getString(R.string.monday),
            context.getString(R.string.tuesday),
            context.getString(R.string.wednesday),
            context.getString(R.string.thursday),
            context.getString(R.string.friday),
            context.getString(R.string.saturday),
            context.getString(R.string.sunday)
        )

        //设置x的字体
        xAxis.position = XAxis.XAxisPosition.BOTTOM
        xAxis.valueFormatter = object : ValueFormatter() {

            override fun getFormattedValue(value: Float): String {
                Log.e(" x 数字格式化 ", "getFormattedValue: ${value.toInt().toString()}", )

                val index=if (value>=6) 6 else value.toInt()-1

                return weekString[index]
            }
        }

        xAxis.granularity = 1f
        xAxis.setDrawGridLines(true)
        xAxis.textSize = 10f
        xAxis.labelCount = 7
        xAxis.axisMinimum = 0.5f
        xAxis.axisLineColor = Color.parseColor("#E5e5e5")
        xAxis.gridColor = Color.parseColor("#E5e5e5")


        axisLeft.setDrawGridLines(true)
        axisLeft.setDrawGridLinesBehindData(true)
        axisLeft.gridColor = Color.parseColor("#E5e5e5")
        axisRight.axisMinimum = 0f
        axisLeft.axisMinimum = 0f
        axisLeft.granularity = 10f
        axisLeft.labelCount = 8
        axisLeft.axisMaximum = 160f
        axisLeft.isEnabled = true
        axisLeft.axisLineColor = Color.parseColor("#E5e5e5")
        axisLeft.textColor = context.getColor2(R.color.transparent)
        //模拟数据
        val yVals = ArrayList<BarEntry>();
        yVals.add(BarEntry(1f, 80f));
        yVals.add(BarEntry(2f, 50f));
        yVals.add(BarEntry(3f, 60f));
        yVals.add(BarEntry(4f, 60f));
        yVals.add(BarEntry(5f, 70f));
        yVals.add(BarEntry(6f, 80f));
        yVals.add(BarEntry(7f, 60f));

        val barDataSet = BarDataSet(yVals, "  ")
        barDataSet.setDrawValues(true)
        barDataSet.valueTextSize = 9f
        val dataSets = ArrayList<IBarDataSet>()
        dataSets.add(barDataSet)
        val data = BarData(dataSets)

        data.setValueFormatter(object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                Log.e(" data 数字格式化 ", "getFormattedValue: ${value.toInt().toString()}", )
                return value.toInt().toString()
            }
        })

        data.barWidth = 0.2f

//        this.data = data

        this.post {
            this.data = data
        }
    }

    val linePaint = Paint().apply {
        style = Paint.Style.FILL
        color = Color.parseColor("#e5e5e5")
        setBorderWidth(1f)
        isAntiAlias = true
    }

    override fun onDraw(canvas: Canvas) {
//        val itemWidth = width / 8
//        val itemheight = height / 8
//        for (i in 0..7) {
//            canvas.drawLine(
//                0f,
//                i * itemheight.toFloat(),
//                width.toFloat(),
//                i * itemheight.toFloat(), linePaint
//            )//水平线
//            canvas.drawLine(
//                i * itemWidth.toFloat(),
//                0f,
//                i * itemWidth.toFloat(),
//                height.toFloat(),
//                linePaint
//            )//竖线
//        }
        super.onDraw(canvas)
    }


}